#ifndef _LOG_WRAPPER_C_H_
#define _LOG_WRAPPER_C_H_

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <securec.h>
#include "log.h"

#undef LOG_TAG
#undef LOG_DOMAIN
#define LOG_TAG "WRAP"
#define LOG_DOMAIN 0xD001406


#define __FILENAME__ (strrchr(__FILE__, '/') ? (strrchr(__FILE__, '/') + 1) : __FILE__)
enum Level { 
    WRAP_LOG_LEVEL_INFO = 0, 
    WRAP_LOG_LEVEL_DEBUG, 
    WRAP_LOG_LEVEL_WARN, 
    WRAP_LOG_LEVEL_ERROR, 
    WRAP_LOG_LEVEL_FATAL 
};

const int MAX_LOG_LENGTH = 2048;

inline static void WrapperLog(int level, const char* format, ...)
{
    char logStr[MAX_LOG_LENGTH];
    va_list args;
    va_start(args, format);
    memset(logStr, 0, MAX_LOG_LENGTH);
    int ret = vsprintf_s(logStr, MAX_LOG_LENGTH, format, args);
    if (ret == -1) { // vsprintf_s failed
        HILOG_ERROR(LOG_CORE, "print log error in vsprintf_s");
        va_end(args);
        return;
    }
    va_end(args);

    switch(level){
        case WRAP_LOG_LEVEL_INFO:
            HILOG_INFO(LOG_CORE, "%{public}s \n", logStr);
            break;
        case WRAP_LOG_LEVEL_DEBUG:
            HILOG_DEBUG(LOG_CORE, "%{public}s \n", logStr);
            break;
        case WRAP_LOG_LEVEL_WARN:
            HILOG_WARN(LOG_CORE, "%{public}s \n", logStr);
            break;
        case WRAP_LOG_LEVEL_ERROR:
            HILOG_ERROR(LOG_CORE, "%{public}s \n", logStr);
            break;
        case WRAP_LOG_LEVEL_FATAL:
            HILOG_FATAL(LOG_CORE, "%{public}s \n", logStr);
            break;
    }

    return;
}

#ifndef ALOG
#define ALOG(level,log_tag,format, ...) \
    do { \
        WrapperLog(level, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#ifndef ALOGE
#define ALOGE(format, ...) \
    do { \
        WrapperLog(WRAP_LOG_LEVEL_ERROR, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#ifndef ALOGI
#define ALOGI(format, ...) \
    do { \
        WrapperLog(WRAP_LOG_LEVEL_INFO, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#ifndef ALOGV
#define ALOGV(format, ...) \
    do { \
        WrapperLog(WRAP_LOG_LEVEL_FATAL, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#ifndef ALOGW
#define ALOGW(format, ...) \
    do { \
        WrapperLog(WRAP_LOG_LEVEL_WARN, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#ifndef ALOGD
#define ALOGD(format, ...) \
    do { \
        WrapperLog(WRAP_LOG_LEVEL_DEBUG, "[%s@%s:%d] " format , __FUNCTION__, __FILENAME__, __LINE__, \
            ##__VA_ARGS__); \
    } while (0)
#endif

#endif